import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutterdemo/0demo/provider/02my_demo/provider_data/common_data.dart';
import 'package:provider/provider.dart';

class TabViewWidget4 extends StatefulWidget {
  final UserModel userModel;

  const TabViewWidget4({Key key, this.userModel}) : super(key: key);

  @override
  _TabViewWidget4State createState() => _TabViewWidget4State();
}

class _TabViewWidget4State extends State<TabViewWidget4> with AutomaticKeepAliveClientMixin {
  UserModel _userModel;

  @override
  Widget build(BuildContext context) {
    print("page 4 build");
    super.build(context);
    _userModel = widget.userModel;
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Card(
        child: Column(
          children: <Widget>[
            Text(_getLatestUserInfo()),
            FlatButton(
              onPressed: _changeUserModel,
              child: Text("修改UserModel"),
            )
          ],
        ),
      ),
    );
  }

  String _getLatestUserInfo() {
    return _userModel == null ? "null" : _userModel.toString();
  }

  void _changeUserModel() {
    context.read<CommonData>().changeUserModel();
  }

  @override
  void dispose() {
    print("4 dispose");
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
